#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_cond_t fa =PTHREAD_COND_INITIALIZER;
pthread_cond_t fb =PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
bool f = false;
void* funA(void*)
{
    while(true)
    {
        pthread_mutex_lock(&lock);
        sleep(1);
        cout<<"我是线程A"<<endl;
        pthread_cond_signal(&fb);
        pthread_cond_wait(&fa,&lock);
        pthread_mutex_unlock(&lock);
    }
    return nullptr;
}
void* funB(void*)
{
    while(true)
    {
        pthread_mutex_lock(&lock);
        sleep(1);
        cout<<"我是线程B"<<endl;
        pthread_cond_signal(&fa);
        pthread_cond_wait(&fb,&lock);
        pthread_mutex_unlock(&lock);
    }
    return nullptr;
}
int main()
{
    pthread_t tid1,tid2;
    pthread_create(&tid1,nullptr,funA,nullptr);
    pthread_create(&tid2,nullptr,funB,nullptr);
    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    return 0;
}